package 栈;

import java.util.Stack;

/**
 * https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/
 * @author 86155
 *
 */
public class _150_逆波兰表达式求值 {

	public int evalRPN(String[] tokens) {
		Stack<Integer> stack = new Stack<>();
		int x, y;
        for (int i = 0; i < tokens.length; i++) {
        	String target = tokens[i];
        	/**
        	 * 可以使用switch case来判断
        	 */
			if (target.equals("+")) {
				x = stack.pop();
				y = stack.pop();
				stack.push(y + x);
			}else if (target.equals("-")) {
				x = stack.pop();
				y = stack.pop();
				stack.push(y - x);
			}else if (target.equals("*") ) {
				x = stack.pop();
				y = stack.pop();
				stack.push(y * x);
			}else if (target.equals("/")) {
				x = stack.pop();
				y = stack.pop();
				stack.push(y / x);
			}else {
				stack.push(Integer.valueOf(target));
			}
		}
		return stack.pop();
    }
}
